package com.mathdroid.crawler.shared;

import java.util.HashMap;
import java.util.LinkedList;

public class Item {
	public String name = null;
	public String image = null;
	public String itemlevel = null;
	public String rarity = null;
	public String use = null;
	public String torheadid = null;

	public LinkedList<HashMap<String, String>> stats = new LinkedList<HashMap<String, String>>();
	
	public HashMap<String, Integer> openModules = new HashMap<String, Integer>();

	public LinkedList<String> moduleNames = new LinkedList<String>();
	public LinkedList<String> moduleilvl = new LinkedList<String>();

	public LinkedList<String> slots = new LinkedList<String>();

	public Item() {
		this.stats.add(new HashMap<String, String>());
	}

	public String toString() {
		String xml = new String();
		int statIndex = 0;

		xml += "<item>\n";
		xml += "\t<torheadid>" + this.torheadid + "</torheadid>\n";
		xml += "\t<name>" + this.name + "</name>\n";
		xml += "\t<imagename>" + this.image + "</imagename>\n";
		xml += "\t<itemlevel>" + this.itemlevel + "</itemlevel>\n";
		xml += "\t<rarity>" + this.rarity + "</rarity>\n";

		for (String s : this.slots) {
			xml += "\t<slot>" + s + "</slot>\n";
		}

		if (this.stats.get(0).size() > 0) {
			xml += "\t<stats>\n";
			for (EItemStat stat : EItemStat.getAllStats()) {
				if (this.stats.get(statIndex).containsKey(stat.getXMLtag())) {
					xml += "\t\t<" + stat.getXMLtag() + ">"
							+ this.stats.get(statIndex).get(stat.getXMLtag())
							+ "</" + stat.getXMLtag() + ">\n";
				}
			}
			xml += "\t</stats>\n";
		}

		if (this.stats.size() > 1 || this.openModules.size() > 0) {
			xml += "\t<modules>\n";

			for (int i = 1; i < this.stats.size(); i++) {
				xml += "\t\t<module>\n";
				xml += "\t\t\t<type>" + this.moduleNames.get(i - 1)
						+ "</type>\n";

				if (this.moduleilvl.get(i - 1) != "1") {
					xml += "\t\t\t<itemlevel>" + this.moduleilvl.get(i - 1)
							+ "</itemlevel>\n";
				}

				if (this.stats.get(i).size() > 0) {
					xml += "\t\t\t<stats>\n";
					for (EItemStat stat : EItemStat.getAllStats()) {
						if (this.stats.get(i).containsKey(stat.getXMLtag())) {
							xml += "\t\t\t\t<" + stat.getXMLtag() + ">"
									+ this.stats.get(i).get(stat.getXMLtag())
									+ "</" + stat.getXMLtag() + ">\n";
						}
					}
					xml += "\t\t\t</stats>\n";
				}

				xml += "\t\t</module>\n";
			}

			for (String s : this.openModules.keySet()) {
				String openModName = s;
				int count = this.openModules.get(s);
				for (int i = 0; i < count; i++) {
					xml += "\t\t<module>\n\t\t\t<type>" + s + "</type>\n\t\t</module>\n";
				}
			}
			xml += "\t</modules>\n";
		}

		xml += "\t<sources>\n\t\t<source>Unknown</source>\n\t</sources>\n";

		if (this.use != null) {
			xml += "\t<use>" + this.use + "</use>\n";
		}

		xml += "</item>\n";

		return xml;
	}
}
